package org.dromara.regionData.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.regionData.domain.bo.InsuranceRegionDataBo;
import org.dromara.regionData.domain.vo.InsuranceRegionDataVo;
import org.dromara.regionData.domain.InsuranceRegionData;
import org.dromara.regionData.mapper.InsuranceRegionDataMapper;
import org.dromara.regionData.service.IInsuranceRegionDataService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 行政区汇总Service业务层处理
 *
 * @author Lion Li
 * @date 2024-12-12
 */
@RequiredArgsConstructor
@Service
public class InsuranceRegionDataServiceImpl implements IInsuranceRegionDataService {

    private final InsuranceRegionDataMapper baseMapper;

    /**
     * 查询行政区汇总
     *
     * @param id 主键
     * @return 行政区汇总
     */
    @Override
    public InsuranceRegionDataVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询行政区汇总列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 行政区汇总分页列表
     */
    @Override
    public TableDataInfo<InsuranceRegionDataVo> queryPageList(InsuranceRegionDataBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<InsuranceRegionData> lqw = buildQueryWrapper(bo);
        Page<InsuranceRegionDataVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的行政区汇总列表
     *
     * @param bo 查询条件
     * @return 行政区汇总列表
     */
    @Override
    public List<InsuranceRegionDataVo> queryList(InsuranceRegionDataBo bo) {
        LambdaQueryWrapper<InsuranceRegionData> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<InsuranceRegionData> buildQueryWrapper(InsuranceRegionDataBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<InsuranceRegionData> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getRegionName()), InsuranceRegionData::getRegionName, bo.getRegionName());
        lqw.eq(StringUtils.isNotBlank(bo.getRegionLevel()), InsuranceRegionData::getRegionLevel, bo.getRegionLevel());
        lqw.eq(bo.getInsuredCount() != null, InsuranceRegionData::getInsuredCount, bo.getInsuredCount());
        lqw.eq(bo.getInsuredAmount() != null, InsuranceRegionData::getInsuredAmount, bo.getInsuredAmount());
        lqw.eq(bo.getClaimCount() != null, InsuranceRegionData::getClaimCount, bo.getClaimCount());
        lqw.eq(bo.getClaimAmount() != null, InsuranceRegionData::getClaimAmount, bo.getClaimAmount());
        lqw.eq(StringUtils.isNotBlank(bo.getInsuranceType()), InsuranceRegionData::getInsuranceType, bo.getInsuranceType());
        lqw.eq(StringUtils.isNotBlank(bo.getLoginIp()), InsuranceRegionData::getLoginIp, bo.getLoginIp());
        lqw.eq(bo.getLoginDate() != null, InsuranceRegionData::getLoginDate, bo.getLoginDate());
        return lqw;
    }

    /**
     * 新增行政区汇总
     *
     * @param bo 行政区汇总
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(InsuranceRegionDataBo bo) {
        InsuranceRegionData add = MapstructUtils.convert(bo, InsuranceRegionData.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改行政区汇总
     *
     * @param bo 行政区汇总
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(InsuranceRegionDataBo bo) {
        InsuranceRegionData update = MapstructUtils.convert(bo, InsuranceRegionData.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(InsuranceRegionData entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除行政区汇总信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
